package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.ProductionWorkOrder;
import com.qf.service.ProductionWorkOrderService;
import com.qf.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 生产工单表(ProductionWorkOrder)表控制层
 *
 * @author makejava
 * @since 2023-09-22 21:11:33
 */
@RestController
@RequestMapping("/api/productionWorkOrder")
public class ProductionWorkOrderController {
    /**
     * 服务对象
     */
    @Autowired
    private ProductionWorkOrderService productionWorkOrderService;
    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @PostMapping("/list/page")
    public R queryByPage(@RequestParam Map<String,String> map) {
        PageHelper.startPage(Integer.parseInt(map.get("pageNum")+""),Integer.parseInt(map.get("pageSize")+""));
        List<ProductionWorkOrder> list = this.productionWorkOrderService.queryByPage(map);
        PageInfo<ProductionWorkOrder> info = new PageInfo<>(list);
        if (info.getList().size() > 0) {
            return R.ok(info);
        }
        return R.fail();
    }
    /**
     * 新增数据
     *
     * @param  productionWorkOrder  实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public R add(ProductionWorkOrder productionWorkOrder, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String value = null;
        for (Cookie cookie : cookies) {
            // String name = cookie.getName();
            // value = cookie.getValue();
            // System.out.println(name +"==>"+ value);
            if ("user".equals(cookie.getName())) {
                value = cookie.getValue();
            }
        }
        productionWorkOrder.setCreateBy(value);//添加时即设置制单人
        int i = this.productionWorkOrderService.insert(productionWorkOrder);
        if (i > 0) {
            return R.ok(i);
        }
        return R.fail();
    }

    /**
     * 编辑数据
     *
     * @param productionWorkOrder 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public R edit(ProductionWorkOrder productionWorkOrder) {
        int i = this.productionWorkOrderService.update(productionWorkOrder);
        if (i > 0) {
            return R.ok(i);
        }
        return R.fail();
    }

    /**
     * 删除数据
     *
     * @param workOrderNumber 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete")
    public R deleteById(String workOrderNumber) {
        int i = this.productionWorkOrderService.deleteById(workOrderNumber);
        if (i > 0) {
            return R.ok(i);
        }
        return R.fail();
    }

    /**
     * 批量删除数据
     *
     * @param list  workOrderNumber 集合
     * @return 删除是否成功
     */
    @PostMapping ("/batchDel")
    public R batchDel(@RequestParam List<String> list) {
        int i = this.productionWorkOrderService.batchDel(list);
        return i > 0 ? R.ok(i) : R.fail();
    }


}

